package com.meizi.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.meizi.codegenerate.tool.service.impl.CrudServiceImpl;
import com.meizi.exception.RenException;
import com.meizi.sys.dao.SysUserMapper;
import com.meizi.sys.dao.SysUserTokenMapper;
import com.meizi.sys.dto.SysUserDto;
import com.meizi.sys.entity.SysUserEntity;
import com.meizi.sys.service.SysUserService;
import com.meizi.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
*
 * @author Administration
 */
@Slf4j
@Service
public class SysUserServiceImpl extends CrudServiceImpl<SysUserMapper, SysUserEntity, SysUserDto> implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserTokenMapper sysUserTokenMapper;

    /**
     *  用户注册
     * @param sysUserDto
     * @return
     */
    @Override
    public Result<Integer> userRegister(SysUserDto sysUserDto) {
        log.info("用户注册参数信息 sysUserDto = {}",sysUserDto);
        if (sysUserDto.getUsername() == null && sysUserDto.getPassword() == null) {
            throw new RenException().setMsg("用户名 密码不能为空");
        }
        // 对邮箱号格式进行校验
        if (sysUserDto.getEmail() == null) {
            throw new RenException().setMsg("邮箱号不能为空");
        }
        String email = sysUserDto.getEmail();
        String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p = Pattern.compile(regEx1);
        Matcher m = p.matcher(email);
        if(!m.matches()) {
            throw new RenException().setMsg("您好！您的邮箱格式填写错误请重新填写！");
        }
        sysUserDto.setSuperAdmin(1);
        sysUserDto.setCreateDate(new Date());
        // 去除密码的空格
        String password = sysUserDto.getPassword();
        String replace = password.replace(" ", "");
        sysUserDto.setPassword(replace);
        SysUserEntity userEntity = new SysUserEntity();
        BeanUtils.copyProperties(sysUserDto, userEntity);
        int insert = sysUserMapper.insert(userEntity);
        return new Result<Integer>().ok(insert);
    }

    /**
     * 修改用户密码
     * @param sysUserDto
     * @return
     */
    @Override
    public Result<Integer> userUpdate(SysUserDto sysUserDto) {
        if (sysUserDto.getId() == null) {
            throw new RenException().setMsg("用户ID 为空");
        }
        SysUserEntity sysUserEntity = new SysUserEntity();
        BeanUtils.copyProperties(sysUserDto, sysUserEntity);
        int i = sysUserMapper.updateById(sysUserEntity);
        return new Result<Integer>().ok(i);
    }

    public SysUserDto findUserById(String userId) {
        SysUserEntity sysUserEntity = sysUserMapper.selectOne(new QueryWrapper<SysUserEntity>()
                .lambda()
                .eq(userId != null, SysUserEntity::getId, userId));
        SysUserDto sysUserDto = new SysUserDto();
        BeanUtils.copyProperties(sysUserEntity, sysUserDto);
        return sysUserDto;
    }

    @Override
    public QueryWrapper<SysUserEntity> getWrapper(Map<String, Object> params) {
        return null;
    }
}
